'use strict';

const { WorkloadModuleBase } = require('@hyperledger/caliper-core');
const helper = require('./helper');


class QueryWorkload extends WorkloadModuleBase {
    constructor() {
        super();
        this.txIndex = 0;
        this.limitIndex = 0;
    }

    async initializeWorkloadModule(workerIndex, totalWorkers, roundIndex, roundArguments, sutAdapter, sutContext) {
        await super.initializeWorkloadModule(workerIndex, totalWorkers, roundIndex, roundArguments, sutAdapter, sutContext);
       
        this.limitIndex = this.roundArguments.assets;
        await helper.createMatrix(this.sutAdapter, this.workerIndex, this.roundArguments);
        
    }

    async submitTransaction() {
        this.txIndex++;
        let matrixNumber = 'client' + this.workerIndex + '_Matrix' + this.txIndex.toString();
    
        const Args = {
            contractId: 'credit-evaluation',
            contractVersion: 'v1.0',
            contractFunction: 'QueryMatrix',
            invokerIdentity: 'Admin',
            contractArguments: [matrixNumber],
            timeout: 30,
            readOnly: true
        };

        if (this.txIndex === this.limitIndex) {
            this.txIndex = 0;
        }

        await this.sutAdapter.sendRequests(Args);
    }
        
        
}

function createWorkloadModule() {
    return new QueryWorkload();
}

module.exports.createWorkloadModule = createWorkloadModule;
